package com.ztgf.shopping.service.shop;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.utils.DateUtils;
import com.ztgf.shopping.mapper.shop.ShopOperationMapper;
import com.ztgf.shopping.mapper.shop.ShopTipMapper;
import com.ztgf.shopping.models.bo.shop.ShopOperationBO;
import com.ztgf.shopping.models.service.shop.IShopOperationService;
import com.ztgf.shopping.models.service.shop.IShopTipService;
import com.ztgf.shopping.models.vo.shop.*;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.ztgf.common.utils.UtilConstants.*;

/*
 * @author: chenyu.wang
 * @date: 2020-08-19
 * @description:店铺运营服务
 */
@DubboService
public class ShopOperationServiceImpl implements IShopOperationService {

    private static final String strDateFormat = "yyyy-MM-dd";

    private static final SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);

    @Autowired
    private ShopOperationMapper shopOperationMapper;
    @Autowired
    private ShopTipMapper shopTipMapper;

    /*
     * @description:获取当前店铺待签收的订单数量和待收货款金额
     * @author: chenyu.wang
     * @date: 2020-08-19
     */
    @Override
    public ShopOperationOneVO queryShopSignedNum(Long shopId) {
        return shopOperationMapper.queryShopSignedNum(shopId);
    }


    /*
     * @description:店铺运营销售额统计树状图
     * @author: chenyu.wang
     * @date: 2020-08-19
     */
    @Override
    public List<ShopOperationVO> queryShopSales(ShopOperationBO bo) {
        if (bo.getType() != null) {
            if (bo.getType() == ONE) {
                //本日
                List<ShopOperationVO> list = new ArrayList<>();
                BigDecimal sales = shopOperationMapper.queryShopThisDaySales(bo);
                ShopOperationVO vo = new ShopOperationVO();
                vo.setSalesPrice(sales);
                vo.setDays(ONE);
                vo.setThisDate(sdf.format(new Date()));
                list.add(vo);
                return list;
            } else if (bo.getType() == TWO) {
                return shopOperationMapper.queryShopThisWeekSales(bo);//本周
            } else {
                return shopOperationMapper.queryShopMonthSales(bo);//本月
            }
        } else {
            if (StringUtils.isNotBlank(bo.getStartTime()) && StringUtils.isNotBlank(bo.getEndTime())) {
                List<ShopOperationVO> list = new ArrayList<>();
                ShopOperationVO vo = shopOperationMapper.queryAutoTimeSales(bo);
                vo.setThisDate(bo.getStartTime() + "~" + bo.getEndTime());
                vo.setDays(ONE);
                list.add(vo);
                return list;
            }
        }
        return null;
    }


    /*
     * @description:店铺运营订单量统计头部数据
     * @author: chenyu.wang
     * @date: 2020-08-20
     */
    @Override
    public ShopOperationTwoVO queryOrderHeader(Long shopId) {
        ShopOperationTwoVO shopOperationTwoVO = shopOperationMapper.queryOrderHeader(shopId);
        if (shopOperationTwoVO!=null){
            // 待确认
            Integer a = shopTipMapper.getSellOrderStateNum(shopId, 0,0);
            // 待付款
            Integer b = shopTipMapper.getTheSellerSPendingPaymentQuantity(shopId,0);
            // 待发货
            Integer d = shopTipMapper.getSellOrderStateNum(shopId, 3,0);

            // 待确认
            Integer a1 = shopTipMapper.getSellOrderStateNum(shopId, 0,1);
            // 待付款
            Integer b1 = shopTipMapper.getTheSellerSPendingPaymentQuantity(shopId,1);
            // 待发货
            Integer d1 = shopTipMapper.getSellOrderStateNum(shopId, 3,1);
            shopOperationTwoVO.setConfirmedOrderNum(a+a1);
            shopOperationTwoVO.setPaymentOrderNum(b+b1);
            shopOperationTwoVO.setDeliveredOrderNum(d+d1);

        }

        return shopOperationTwoVO;
    }


    /*
     * @description:店铺运营订单量统计树状图
     * @author: chenyu.wang
     * @date: 2020-08-20
     */
    @Override
    public List<ShopOperationThreeVO> queryShopOrderTree(ShopOperationBO bo) {
        if (bo.getType() != null) {
            if (bo.getType() == ONE) {
                //本日
                List<ShopOperationThreeVO> list = new ArrayList<>();
                ShopOperationThreeVO vo = shopOperationMapper.queryShopOrderVolumeThisDay(bo);
                vo.setDays(ONE);
                vo.setThisDate(sdf.format(new Date()));
                list.add(vo);
                return list;
            } else if (bo.getType() == TWO) {
                return shopOperationMapper.queryShopOrderVolumeThisWeek(bo);//本周
            } else {
                return shopOperationMapper.queryShopOrderVolumeThisMonth(bo);//本月
            }
        } else {
            if (StringUtils.isNotBlank(bo.getStartTime()) && StringUtils.isNotBlank(bo.getEndTime())) {
                List<ShopOperationThreeVO> list = new ArrayList<>();
                ShopOperationThreeVO vo = shopOperationMapper.queryShopOrderVolumeAutoTime(bo);
                vo.setThisDate(bo.getStartTime() + "~" + bo.getEndTime());
                vo.setDays(ONE);
                list.add(vo);
                return list;
            }
        }
        return null;
    }

    /*
     * @description:
     * @author: chenyu.wang
     * @date: 2020-08-20
     */
    @Override
    public Page<ShopOperationFourVO> queryShopOrderToDayList(Integer pageNo, Integer pageSize, Long shopId) {
        Page<ShopOperationFourVO> page = new Page<>(pageNo, pageSize);
        List<ShopOperationFourVO> list = shopOperationMapper.queryShopOrderToDayList(page, shopId);
        page.setRecords(list);
        return page;
    }


    /*
     * @description:店铺运营PV/UV，浏览量/访客数树状图
     * @author: chenyu.wang
     * @date: 2020-08-20
     */
    @Override
    public ShopOperationEightVO queryShopPVUVTree(ShopOperationBO bo) {
        ShopOperationEightVO real = new ShopOperationEightVO();
        if (bo.getType() != null) {
            if (bo.getType() == ONE) {
                List<ShopOperationSevenVO> list = new ArrayList<>();
                ShopOperationSevenVO vo = shopOperationMapper.queryShopPageViewsToday(bo);//今日浏览量
                list.add(vo);
                vo.setDays(ONE);
                vo.setThisDate(sdf.format(new Date()));
                real.setPageViews(list);
                List<ShopOperationSevenVO> vos = new ArrayList<>();
                ShopOperationSevenVO shopOperationSevenVO = shopOperationMapper.queryShopVisitorsToday(bo);//今日访客量
                shopOperationSevenVO.setThisDate(sdf.format(new Date()));
                shopOperationSevenVO.setDays(ONE);
                vos.add(shopOperationSevenVO);
                real.setVisitors(vos);
            } else if (bo.getType() == TWO) {
                List<ShopOperationSevenVO> list = shopOperationMapper.queryShopPageViewsThisWeek(bo);//本周浏览量
                real.setPageViews(list);
                List<ShopOperationSevenVO> vos = shopOperationMapper.queryShopVisitorsThisWeek(bo);//本周访客量
                real.setVisitors(vos);
            } else {
                List<ShopOperationSevenVO> list = shopOperationMapper.queryShopPageViewsThisMonth(bo);//本月浏览量
                real.setPageViews(list);
                List<ShopOperationSevenVO> vos = shopOperationMapper.queryShopVisitorsThisMonth(bo);
                real.setVisitors(vos);
            }
        } else {
            if (StringUtils.isNotBlank(bo.getStartTime()) && StringUtils.isNotBlank(bo.getEndTime())) {
                List<ShopOperationSevenVO> list = new ArrayList<>();
                ShopOperationSevenVO vo = shopOperationMapper.queryShopPageViewsAutoTime(bo);//自定义时间浏览量
                vo.setDays(ONE);
                vo.setThisDate(bo.getStartTime() + "~" + bo.getEndTime());
                list.add(vo);
                real.setPageViews(list);
                List<ShopOperationSevenVO> vos = new ArrayList<>();
                ShopOperationSevenVO shopOperationSevenVO = shopOperationMapper.queryShopVisitorsAutoTime(bo);
                shopOperationSevenVO.setDays(ONE);
                shopOperationSevenVO.setThisDate(bo.getStartTime() + "~" + bo.getEndTime());
                vos.add(shopOperationSevenVO);
                real.setVisitors(vos);
            }
        }
        return real;
    }

    /*
     * @description:店铺运营pv/uv统计头部数据
     * @author: chenyu.wang
     * @date: 2020-08-24
     */
    @Override
    public ShopOperationTenVO queryPVVUHeader(Long shopId) {
        ShopOperationTenVO vo = new ShopOperationTenVO();
        Date date = DateUtils.dateFormat(DateUtils.addDay(DateUtils.getCurrentDate(), -1), DateUtils.DATE_FORMAT_DATEDAY);
        String s = DateUtils.dateToStr(date, DateUtils.DATE_FORMAT_DATEDAY);
        vo.setYesterDayPageviews(shopOperationMapper.queryYesterDaySum(shopId,s));
        vo.setYesterDayVisitors(shopOperationMapper.queryYesterDayShopNum(shopId,s));

        vo.setYesterDayGoodsPageviews(shopOperationMapper.queryYesterDayGoodsNum(shopId,s));
        vo.setYesterDayGoodsVisitors(shopOperationMapper.queryYesterDayGoodsIPNum(shopId,s));
        return vo;
    }

    @Override
    public ShopOperationEightVO queryGoodsPVUVTree(ShopOperationBO bo) {
        ShopOperationEightVO real = new ShopOperationEightVO();
        if (bo.getType() != null) {
            if (bo.getType() == ONE) {
                List<ShopOperationSevenVO> list = new ArrayList<>();
                ShopOperationSevenVO vo = shopOperationMapper.queryGoodsPageViewsToday(bo);//今日浏览量
                list.add(vo);
                vo.setDays(ONE);
                vo.setThisDate(sdf.format(new Date()));
                real.setPageViews(list);
                List<ShopOperationSevenVO> vos = new ArrayList<>();
                ShopOperationSevenVO shopOperationSevenVO = shopOperationMapper.queryGoodsVisitorsToday(bo);//今日访客量
                shopOperationSevenVO.setThisDate(sdf.format(new Date()));
                shopOperationSevenVO.setDays(ONE);
                vos.add(shopOperationSevenVO);
                real.setVisitors(vos);
            } else if (bo.getType() == TWO) {
                List<ShopOperationSevenVO> list = shopOperationMapper.queryGoodsPageViewsThisWeek(bo);//本周浏览量
                real.setPageViews(list);
                List<ShopOperationSevenVO> vos = shopOperationMapper.queryGoodsVisitorsThisWeek(bo);//本周访客量
                real.setVisitors(vos);
            } else {
                List<ShopOperationSevenVO> list = shopOperationMapper.queryGoodsPageViewsThisMonth(bo);//本月浏览量
                real.setPageViews(list);
                List<ShopOperationSevenVO> vos = shopOperationMapper.queryGoodsVisitorsThisMonth(bo);
                real.setVisitors(vos);
            }
        } else {
            if (StringUtils.isNotBlank(bo.getStartTime()) && StringUtils.isNotBlank(bo.getEndTime())) {
                List<ShopOperationSevenVO> list = new ArrayList<>();
                ShopOperationSevenVO vo = shopOperationMapper.queryGoodsPageViewsAutoTime(bo);//自定义时间浏览量
                vo.setDays(ONE);
                vo.setThisDate(bo.getStartTime() + "~" + bo.getEndTime());
                list.add(vo);
                real.setPageViews(list);
                List<ShopOperationSevenVO> vos = new ArrayList<>();
                ShopOperationSevenVO shopOperationSevenVO = shopOperationMapper.querygoodsVisitorsAutoTime(bo);
                shopOperationSevenVO.setDays(ONE);
                shopOperationSevenVO.setThisDate(bo.getStartTime() + "~" + bo.getEndTime());
                vos.add(shopOperationSevenVO);
                real.setVisitors(vos);
            }
        }
        return real;
    }
}
